package com.sw.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sw.model.Point;
import com.sw.render.TileRender;
import com.sw.render.png.PngTileRender;

public class Render extends HttpServlet {

	private TileRender tileRender;

	@Override
	public void init() throws ServletException {
		super.init();
		tileRender = new PngTileRender();
	}

	@Override
	public void destroy() {
		super.destroy();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter out = resp.getWriter();
		StringBuilder msg = new StringBuilder();
		Map<String, String[]> mapP = req.getParameterMap();
		msg.append("reguest parameters : ");
		for (String parameter : mapP.keySet()) {
			msg.append(parameter + "=" + mapP.get(parameter)[0] + ",");
		}
		msg.append("\n");
		Point point = null;
		try {
			point = new Point(req.getParameterMap());
			msg.append("point : " + point);
			tileRender.renderTile(point,"");
		} catch (Exception e) {
			System.out
					.println("Exception EagleServlet.doPost(,) at Point initialization : "
							+ e);
			e.printStackTrace();
		}
		out.write(msg.toString());
		out.close();
	}

}